﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using org.dicomcs.data;
using org.dicomcs.dict;
using System.Diagnostics;

namespace waveform_test
{
    public partial class DICOMPropertiesExplorerForm : Form
    {
        private Dataset ds;
        private SQElement sq;
        private List<int> expandable;

        public DICOMPropertiesExplorerForm()
        {
            InitializeComponent();
            this.expandable = new List<int>();
        }

        public DICOMPropertiesExplorerForm(Dataset ds)
            : this()
        {
            this.ds = ds;
            ViewDatasetDetails();
        }

        public DICOMPropertiesExplorerForm(SQElement sq)
            : this()
        {
            this.sq = sq;
            this.Text += " " + VRs.ToString(sq.vr()) + " - " + Tags.ToHexString(sq.tag());
            ViewSQDetails();
        }

        private void ViewSQDetails()
        {
            propertiesDataGridView.Columns.Add("itemColumn", "Item");
            propertiesDataGridView.Columns.Add("viewMoreColumn", "View More");
            
            for (int i = 0; i < sq.vm(); ++i)
            {
                int row = propertiesDataGridView.Rows.Add("Item-" + (i + 1).ToString(), "More");
                propertiesDataGridView.Rows[row].Tag = sq.GetItem(i);
                expandable.Add(row);
            }

            propertiesDataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
        }

        private void ViewDatasetDetails()
        {
            propertiesDataGridView.Columns.Add("orderColumn", "Order");
            propertiesDataGridView.Columns.Add("tagColumn", "Tag");
            propertiesDataGridView.Columns.Add("vrColumn", "Vr");
            propertiesDataGridView.Columns.Add("valueColumn", "Value");
            propertiesDataGridView.Columns.Add("valueColumn", "Type");
            propertiesDataGridView.Columns.Add("viewMoreColumn", "View More");

            int order = 1;
            foreach (DcmElement element in ds.M_list)
            {
                int row = propertiesDataGridView.Rows.Add(order, Tags.ToHexString(element.tag()), VRs.ToString(element.vr()), element.Get64ValueAsString(), element.GetType().ToString(), (element.vr() == VRs.SQ) ? "More" : string.Empty);
                
                propertiesDataGridView.Rows[row].Tag = element;
                
                if (element.vr() == VRs.SQ)
                {
                    expandable.Add(row);
                }
                order++;
                
            }

            propertiesDataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
        }

        private void propertiesDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (expandable.Contains(e.RowIndex))
            {
                DICOMPropertiesExplorerForm explorer;
                object untypedElement = ((DataGridView)sender).Rows[e.RowIndex].Tag;
                if (untypedElement is SQElement)
                {
                    SQElement element = (SQElement)untypedElement;
                    explorer = new DICOMPropertiesExplorerForm(element);
                }
                else if (untypedElement is Dataset)
                {
                    Dataset element = (Dataset)untypedElement;
                    explorer = new DICOMPropertiesExplorerForm(element);
                }
                else
                    throw new ArgumentOutOfRangeException("Cannot create explorer for type " + untypedElement.GetType().ToString());
                explorer.ShowDialog();
            }
        }

        private void propertiesDataGridView_MouseUp(object sender, MouseEventArgs e)
        {
            DataGridView.HitTestInfo hitTestInfo;
            if (e.Button == MouseButtons.Right)
            {
                hitTestInfo = propertiesDataGridView.HitTest(e.X, e.Y);
                if (hitTestInfo.Type == DataGridViewHitTestType.Cell && !expandable.Contains(hitTestInfo.RowIndex))
                {
                    propertiesDataGridView.ClearSelection();
                    propertiesDataGridView.CurrentCell = propertiesDataGridView.Rows[hitTestInfo.RowIndex].Cells[hitTestInfo.ColumnIndex];
                    propertyContextMenuStrip.Show(propertiesDataGridView, e.Location);
                }
            }
        }

        private void copiarValorAlPortapapelesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DcmElement element = (DcmElement)propertiesDataGridView.CurrentRow.Tag;
            Debug.WriteLine(element.tag().ToString() + " " + Tags.WaveformData.ToString()); 
            
            //if (element.tag() == Tags.WaveformData) //0x54001010
                
            Clipboard.SetText(element.GetFullValueAsString());
        }
    }
}
